﻿using System;
using Com.Lancetinc.Evs.Resources;
using MahApps.Metro.IconPacks;
using System.Windows;
using System.Windows.Input;

namespace Com.Lancetinc.Evs.Controls
{
    /// <summary>
    /// 首页下方的控制按钮
    /// </summary>
    public partial class MainControlButton
    {
        #region Icon

        public static readonly DependencyProperty IconProperty
            = DependencyProperty.Register(nameof(Icon),
                typeof(string),
                typeof(MainControlButton),
                new PropertyMetadata(null, OnIconChanged));

        private static void OnIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as MainControlButton).UpdateIcon();
        }

        public string Icon
        {
            get => (string)this.GetValue(IconProperty);
            set => this.SetValue(IconProperty, value);
        }

        #endregion

        #region Text

        public static readonly DependencyProperty TextProperty
            = DependencyProperty.Register(nameof(Text),
                typeof(string),
                typeof(MainControlButton),
                new PropertyMetadata(string.Empty, OnTextChanged));

        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as MainControlButton).UpdateText();
        }

        public string Text
        {
            get => (string)this.GetValue(TextProperty);
            set => this.SetValue(TextProperty, value);
        }

        #endregion

        public MainControlButton()
        {
            InitializeComponent();
        }

        /// <inheritdoc />
        protected override void OnIsPressedChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnIsPressedChanged(e);
            UpdateIcon();
        }

        /// <inheritdoc />
        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            UpdateIcon();
        }

        /// <inheritdoc />
        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            UpdateIcon();
        }

        private void UpdateIcon()
        {
            if (Icon == null) return;

            if (Enum.TryParse<PackIconLevsKind>(Icon, out var kind))
            {
                ImgIcon.Kind = kind;
            }
        }

        private void UpdateText()
        {
            this.TxbTitle.Text = this.Text;
        }
    }
}